AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "apc_engine_start" )
util.PrecacheSound( "apc_engine_stop" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Energy_Increment = 5
local Coolant_Increment = 5

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.val2 = 0
	self.damaged = 0
	LS_RegisterEnt(self.Entity)
	self.timer = CurTime() + 1
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end

	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Entity:EmitSound( "apc_engine_start" )
				self.Active = 1
				self.running = 1
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		else
			if ( self.Active == 1 ) then
				self.Entity:StopSound( "apc_engine_start" )
				self.Entity:EmitSound( "apc_engine_stop" )
				self.Active = 0
				self.running = 0
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
	if ((self.Active == 1) and (math.random(1, 10) <= 3)) then
		self.Active = 0
		self.Entity:StopSound( "apc_engine_start" )
		self.Entity:EmitSound( "apc_engine_stop" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound( "apc_engine_start" )
end


function ENT:Output()
	return 1
end

function ENT:Pump_Heat()
	local energy = RD_GetResourceAmount(self, "energy")
	local coolant = RD_GetResourceAmount(self, "coolant")

	if (((coolant <= 0) and (self.environment.temperature > FairTemp)) or ((energy <= 0) and (self.environment.temperature < FairTemp))) then
		self.running = 0
	else
		self.running = 1
	end
	local found = 0
	local e_dec = 0
	local c_dec = 0
	local tAllPlayers = player.GetAll()
	for _, pPlayer in pairs( tAllPlayers ) do
		if pPlayer.Entity:GetPos():Distance(self.Entity:GetPos()) < self.range then
			found = 1
			if ( self.running == 1 ) then
				if (pPlayer.suit.energy < 100) then
					if ( (100 - pPlayer.suit.energy) < energy ) then
						e_dec = (e_dec + (100 - pPlayer.suit.energy))
						pPlayer.suit.energy = 100
					elseif (energy > 0) then
						pPlayer.suit.energy = ( pPlayer.suit.energy + energy )
						e_dec = (e_dec + energy)
					end
				end
				if (pPlayer.suit.coolant < 100) then
					if ( (100 - pPlayer.suit.coolant) < coolant ) then
						c_dec = (c_dec + (100 - pPlayer.suit.coolant))
						pPlayer.suit.coolant = 100
					elseif (coolant > 0) then
						pPlayer.suit.coolant = ( pPlayer.suit.coolant + coolant )
						RD_ConsumeResource(self, "coolant", coolant)
						c_dec = (c_dec + coolant)
					end
				end
			end
		end
	end
	
	if (found == 1) then
		if (self.running == 1) then
			if (self.environment.atmosphere > 0) then
				if (self.environment.temperature > FairTemp) then
					c_dec = (c_dec + (math.ceil((self.environment.temperature - FairTemp) / 2)))
				elseif (self.environment.temperature < FairTemp) then
					e_dec = (e_dec + (math.ceil((FairTemp - self.environment.temperature) / 2)))
				end
			else
				RD_ConsumeResource(self, "energy", 2)
				e_dec = (e_dec + 2)
			end
			
			if (c_dec > coolant) then
				RD_ConsumeResource(self, "coolant", coolant)
			else
				RD_ConsumeResource(self, "coolant", c_dec)
			end
			
			if (e_dec > energy) then
				RD_ConsumeResource(self, "energy", energy)
			else
				RD_ConsumeResource(self, "energy", e_dec)
			end
		else
			self.Entity:EmitSound( "common/warning.wav" )
		end
	end

	return
end

function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Heat Exchanger (OFF)\nCoolant: " .. self.val1 .. "\nEnergy: " .. self.val2 )
	else
		self:SetOverlayText( "Heat Exchanger (ON)\nCoolant: " .. self.val1 .. "\nEnergy: " .. self.val2 )
	end

	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "coolant")
	self.val2 = RD_GetResourceAmount(self, "energy")
	if ( self.Active == 1 ) then self:Pump_Heat() end
	self.timer = CurTime() +  1
end



function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Entity:EmitSound( "apc_engine_start" )
			self.Active = 1
			self.running = 1
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
		else
			self.Entity:StopSound( "apc_engine_start" )
			self.Entity:EmitSound( "apc_engine_stop" )
			self.Active = 0
			self.running = 0
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
		end
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
